﻿$(document).ready(function(){
	getSource();
	initEvent();
});
/**
 * 加载数据
 */
var myPagination;
function initEvent(){
	  $("#submitbutton").click(function(){
		   var formData = $("#queryForm").serialize(); //序列化表单
		   formData = decodeURIComponent(formData, true); //解码
		   formData += "&fresourcetype=数据&fdeleted=1";
		   $.fn.debug("开始指定加载");
		   myPagination.onLoad({param:formData});
		   $.fn.debug("结束指定加载");
	});
	$("#order").click(function(){
		if($('#checked').val()){
			$('#checked').val('');
			$("span[class=orderTime]").css("backgroundPosition","100% -2100px");
		}else{
			$('#checked').val('1');
			$("span[class=orderTime]").css("backgroundPosition","100% -2320px");
		}
		$("#submitbutton").click();
	});
}
function getSource(){
	var formData = "&fresourcetype=数据&fdeleted=1";
	myPagination = $("div[class=holder]").myPagination({
		currPage: 1,
		pageNumber: 15,
		limit :10,
		cssStyle:'grayr',
		panel:{
			tipInfo_on:true,
			tipInfo_css:{
				width:"22px",height:'12px'
			}
		},
		ajax: {
		  on: true,
		  url: "resourceList",
		  dataType: 'json',
		  param:formData,
		  ajaxStart:function(){
			  ZENG.msgbox.show(" 正在加载中，请稍后...", 6, 10000);
		  },onClick:function(page){
			  $.fn.debug(page);
		  },
		  callback:function(data){
			  ZENG.msgbox.hide(); //隐藏加载提示
			  $.fn.debug(data.source);
			  bindGridList(data);
		  }
		}
	});
}
/***
 * 绑定音频
 * @param data
 * @returns {String}
 */
function bindGridList(data){
	$("#total").html('总共找到'+data.total+'条数据');
	var html= '';
	if(data.total>0){
		$.each(data.source,function(i,resource){
			html += '<tr id="web'+i+'" class="tableContent" onclick="ischecked(this)">';
			html +='<td style="padding-left: 5px;"><input type="checkbox" name="web" onchange="checkedInput(this)" onclick="ischecked(document.getElementById(\'web'+i+'\'))" id="td'+ i +'" value="'+resource.id+'"/></td>';
			html += "<td>"+resource.fname+"</td>";
			html += "<td>"+resource.fresourcetype+"</td>";
			html += "<td>"+resource.fcount+'</td>';
			html += "<td>"+resource.faddperson+'</td>';
			html += "<td>"+resource.faddtime+'</td></tr>';
		});
	}else{
		html = "<tr><td colspan='6' align='center'>暂无数据</td></tr>";
	}
	$("#audios").html(html);
}
/**
 * 选中子元素
 * @param obj
 */
function check(obj){
	if(obj.checked){
		var checkbox = $("[name='web']");
		checkbox.attr("checked",'true');//全选 
		$("[class='tableContent']").css("background-color","#D8F79D");
	}else{
		$("[name='web']").removeAttr("checked");//取消全选 
		$("[class='tableContent']").css("background-color","");
	}
}
/**
 * 当前选中项
 * @param obj
 */
function checkedInput(obj){
	var id = obj.id.replace("td","");
	if(obj.checked)
		$("#web"+id).css("background-color","#D8F79D");
	else
		$("#web"+id).css("background-color","");
}
/**
 * 单击行时，选中的则取消，未选中的则选中
 * @param obj
 */
function ischecked(obj){
	var id = obj.id;
	var trObj = $("#"+id);
	var tdId = "td"+id.replace("web","");
	var objCheck = $("#"+tdId);
	if(objCheck.attr("checked")){
		objCheck.removeAttr("checked");//取消选中 
		trObj.css("background-color","");
	}else{
		objCheck.attr("checked",'true');//选中
		trObj.css("background-color","#D8F79D");
	}
}
/**
 * 获取所有选中的id
 * @returns {Array}
 */
function checkedInfo(){
	var ids = new Array();
	$("[name='web'][checked]").each(function(){  
		ids.push($(this).val());  
	}); 
	return ids;  
}
/**
 * 加载数据
 * @param id
 */
function editAjax(id){
	var data = {"id":id};
	$.ajax({
		url:"queryResourceById",
		data:data,
		type:"post",
		dataType:"json",
		success:function(data){
			bindGridEdit(data);
		},
		error:function(data){
			$.growlUI('数据加载出错'); 
		}
	});
}
/**
 * 删除站点信息
 */
function checkedDeletedResource(flag,isDeleted){
	var ids = checkedInfo();
	if(ids.length > 0){
		var s = "";
	    for(var i=0;i<ids.length ;i++){
	        s +=ids[i]+",";
	    }
	    if(s != ""){
	    	s = s.substring(0,s.length-1) ;
	    }
		var data = {"ids":s,"flag":flag,"isDelete":isDeleted};
		$.ajax({
			url:"checkedDeleted",
			data:data,
			type:"post",
			success:function(data){
				$.growlUI('成功操作数据'+data+'条');
//				getSource(this.checked,'1');
				$('a[title=1]').click();
			},
			error:function(data){
				$.growlUI('数据操作失败'); 
			}
		});
		$("#AllCheck").attr("checked",false);
	}else{
		var html = "<div style='vertical-align: middle;'><img src='/EMSP_CMS/images/box/icon_alert.gif' alt='提示'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请选择要操作的信息</div>";
		var	footerhtml='<input type="image" alt="Cancel" src="/EMSP_CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'deleteHtmlcloseBtn\').click();"/>';
		Box("deleteHtml",10,10,html,footerhtml,901);
	}
}
/**
 * 删除数据
 */
function deletedResource(){
	if(confirm('您确定删除？删除后无法恢复')){
		var ids = checkedInfo();
		if(ids.length > 0){
			var s = "";
		    for(var i=0;i<ids.length ;i++){
		        s +=ids[i]+",";
		    }
		    if(s != ""){
		    	s = s.substring(0,s.length-1) ;
		    }
			var data = {"ids":s};
			$.ajax({
				url:"batchDelete",
				data:data,
				type:"post",
				success:function(data){
					$.growlUI('成功删除数据'+data+'条');
//					getSource(this.checked,1);
					$('a[title=1]').click();
				},
				error:function(data){
					$.growlUI('数据删除失败'); 
				}
			});
		}else{
			var html = "<div style='vertical-align: middle;'><img src='/EMSP_CMS/images/box/icon_alert.gif' alt='提示'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请选择要删除的信息</div>";
			var	footerhtml='<input type="image" alt="Cancel" src="/EMSP_CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'deleteHtmlcloseBtn\').click();"/>';
			Box("deleteHtml",10,10,html,footerhtml,901);
		}
	}
}